﻿//
// Created by gogoqiu on 2021/8/25.
//

#include "PreferencesFrame.h"
#include "InputDialog.h"
#include <vector>
#include "../MyTagApp.h"
#include "../MyObj.h"
#include "../data/PreferenceAdapter.h"
#include "../ids.h"

using namespace std;

//extern MyApp *myApp;

struct DataBundle{
    PreferencesFrame* preferencesFrame;
    int clickIdx;
};


BEGIN_EVENT_TABLE ( PreferencesFrame, wxFrame )
    EVT_MENU ( ACTION_EVENT, PreferencesFrame::OnActionEvent )
END_EVENT_TABLE()


//如何去掉
//动态添加配置?
/**
 * wxDEFAULT_FRAME_STYLE
 * wxDEFAULT_FRAME_STYLE ^ wxMAXIMIZE_BOX ^ wxRESIZE_BORDER
 *
 * @param _myTagApp
 */
PreferencesFrame::PreferencesFrame( wxWindow *parent, MyTagApp * _myTagApp )
        : wxFrame( parent, wxID_ANY, "配置", wxDefaultPosition, wxDefaultSize,
                  wxDEFAULT_FRAME_STYLE ^ wxMAXIMIZE_BOX ^ wxRESIZE_BORDER )
{
    this->myTagApp = _myTagApp;
    auto *menuFile = new wxMenu;
    //
    menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
                     "Help string shown in status bar for this menu item");
    menuFile->AppendSeparator();
    menuFile->Append(wxID_EXIT);
    auto *menuHelp = new wxMenu;
    menuHelp->Append(wxID_ABOUT);
    auto *menuBar = new wxMenuBar;
    menuBar->Append(menuFile, "&File");
    menuBar->Append(menuHelp, "&Help");
    SetMenuBar( menuBar );
    CreateStatusBar();
    SetStatusText("Welcome to wxWidgets!");
    //
    Bind(wxEVT_MENU, &PreferencesFrame::OnHello, this, ID_Hello);
    Bind(wxEVT_MENU, &PreferencesFrame::OnAbout, this, wxID_ABOUT);
    Bind(wxEVT_MENU, &PreferencesFrame::OnExit, this, wxID_EXIT);
    SetClientSize(wxSize(350,500));
    //
    dialogRootBoxSizer = new wxBoxSizer(wxVERTICAL);
    m_listbox = new wxScrolledWindow(this, wxID_ANY,
                                     wxDefaultPosition, wxDefaultSize, wxHSCROLL | wxVSCROLL );
    //m_listbox: panelRootBoxSizer
    //panels: innerSizer
    m_listbox->SetScrollRate(5, 5 );
    //
    dialogRootBoxSizer->Add(m_listbox, 1, wxEXPAND, 0 );
    this->SetSizer(dialogRootBoxSizer);
    //m_listbox->SetBackgroundColour(wxColour(0, 128, 192 ) );
    //return;
    //wxBoxSizer *BoxSizer = new wxBoxSizer(wxHORIZONTAL);
    //panelRootBoxSizer = new wxBoxSizer(wxVERTICAL);

    //
    //BuildView();

    mPreferenceAdapter = new PreferenceAdapter( myTagApp, this, m_listbox );

    //connect this::close to this::dismiss
    //https://blog.csdn.net/sq8706/article/details/119087505
    //wxWidgets 关闭按钮
    //动态设置的
    Bind(wxEVT_CLOSE_WINDOW,wxCloseEventHandler(PreferencesFrame::OnClose),this);
    //bind the request data to the this frame on the data
    //PreferenceAdapter::OnActionEvent ( wxCommandEvent & event )
    /*
     * void Bind(const EventTag &eventType,
              void (Class::*method)(EventArg &),
              EventHandler *handler,
              int winid = wxID_ANY,
    EVT_MENU ( ACTION_EVENT, MainFrame::OnActionEvent )
     */
    //Bind( ACTION_EVENT, wxCommandEventHandler(PreferencesFrame::OnActionEvent ), this );
    request();
}

/*
 *
 */
void PreferencesFrame::request(){
    //?
    vector< PreferenceView*> items = myTagApp->getPreferenceCore()->getPreferenceViews();
    //?
    mPreferenceAdapter->setItemsWithNotify( items );
    //this->SetSizer(dialogRootBoxSizer);
    //Layout();
}

void PreferencesFrame::OnClose( wxCloseEvent&  event ){
    this->Show( false );
}

PreferencesFrame::~PreferencesFrame(){
    for( int i=0;i <panels.size(); i++ ){
        //panels.at(i)->Disconnect( wxEVT_LEFT_DOWN, wxMouseEventHandler( PreferencesFrame::OnClickItem ), NULL, this );
    }
}

void PreferencesFrame::OnExit(wxCommandEvent& event)
{
    Close(true);
}

void PreferencesFrame::OnAbout(wxCommandEvent& event)
{
    wxMessageBox("This is a wxWidgets Hello World example",
                 "About Hello World", wxOK | wxICON_INFORMATION);
}

void PreferencesFrame::OnHello(wxCommandEvent& event)
{
    wxLogMessage("Hello world from wxWidgets!");
}
/*
ViewHolder onCreateViewHolder ( wxPanel parent, int viewType )
{
    return new ViewHolder( inflater.inflate ( R.layout.widget_complain_item, parent, false ) );
}

void onBindViewHolder ( ViewHolder holder, int position )
{
    holder.update( items.get(position));
}
*/

/*
 * 规则
 */
void PreferencesFrame::BuildView() {
    //如何居中窗口
    SetClientSize(wxSize(350,500));

    vector<PreferenceView*>::iterator _iterator;
    /*
     * how to build the preference views
     * myApp::onInit::init the PreferenceCore
     * myApp->PreferenceCore
     * PreferencesFrame::myTagApp::PreferenceCore
     * PreferenceCore:
     *  inner List key items
     *  query sqlite items
     *
    */
    vector<PreferenceView*> views = myTagApp->getPreferenceCore()->getPreferenceViews();
    wxObject * object;
    /*
     **/
    for(_iterator = views.begin();
        _iterator!=views.end(); _iterator++ ) {

        //innerSizer->Fit( m_panel );
        //create the panel
        m_panel = new ListItemPanel( this, _iterator.operator*() );
        /*
         * root
         *  panels list root:   wxScrolledWindow to set size the panelsListSize
         *      panels
         *          panel root
         *              controls
         *          ...
         *
         */
        panelRootBoxSizer->Add(m_panel, 0,
                       wxEXPAND|wxALL, 15);
        panels.push_back(m_panel);

        //??
        object = new MyObj( _iterator.operator*() );
        //???
        m_panel->Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler( PreferencesFrame::OnClickItem ),
                         object, this );
    }
    //??
    m_listbox->SetSizer( panelRootBoxSizer );
    m_listbox->Layout();
    //m_listbox->AddChild( bt_add );
    this->SetSizer(dialogRootBoxSizer);
    Layout();
}

//Panel onClick
void PreferencesFrame::OnClickItem(wxMouseEvent& event){
    auto *myObj = reinterpret_cast<MyObj *>(event.GetEventUserData());
    auto *preferenceView = reinterpret_cast<PreferenceView*>(myObj->data);
    switch( preferenceView->getType() ){
        case PT_INPUT:
            auto *dialog = new InputDialog( this, preferenceView );
            if ( dialog->ShowModal() == wxID_OK ){
                if( myTagApp->getPreferenceCore()->PutValue( preferenceView->getKey(), preferenceView->getValue())==0 ){
                    preferenceView->getControl()->SetLabel( preferenceView->getValue() );
                }
            }
            break;
    }
    return;
}

void PreferencesFrame::OnActionEvent(wxCommandEvent &event) {
    mPreferenceAdapter->OnActionEvent( event );
}

//MyPreferencesFrame